define(function(require){

    require('../../../c/base/index');

    require('../../../c/nav/index');


    var Search = {
        init : function() {
            //初始化菜单选中效果
            $(".fa-circle-o").each(function(){
                if($(this).parents("a").text() == "用户查询"){
                    $(this).parents("a").parents("li").addClass("active");
                }
            });
            $("#panel").hide();
            this.bindEvent();
        },

        bindEvent : function() {
            var self = this;

            $('#mobileForm').bootstrapValidator({
                message: '输入错误',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    sMobile: {
                        message: '手机号码输入错误',
                        validators: {
                            notEmpty: {
                                message: '手机号码不能为空'
                            },
                            phone: {
                                country: 'CN',
                                message: '请输入正确的电话格式'
                            }
                        }
                    }
                }
            });

            $("#get").click(function () {
                var obj = $("#mobileForm").data('bootstrapValidator');
                obj.validate();
                if (obj.isValid()) {
                    $("#panel").hide();
                    $("#showResults").empty();
                    var mobile = $(".form-control").val();
                    self.getUserByMobile(mobile);
                }
            });
            $("#detail").click(function () {
                var mobile = $("#userMobile").val();
                var userId = $("#userId").val();
                window.location.href = "./userDetail.html?mobile=" + mobile + "&userId=" + userId;
            });
        },

        getUserByMobile : function(mobile) {
            CRM.request({
                url: CRM.getOuterUrl('/member/getUserbyMobile.do?mobile=' + mobile),
                type: 'get',
                error: function (error) {
                    var dis = "出错了!请重试";
                    $("#showResults").append('<div class="alert alert-danger">' + dis
                        + '</div>');
                },
                success: function (data) {
                    if (data.status) {
                        $("#mobile").text(data.entry.mobile);
                        $("#nick").text(data.entry.userNick);
                        $("#userMobile").val(data.entry.mobile);
                        $("#userId").val(data.entry.userId);
                        $("#panel").show();
                    } else {
                        $("#showResults").append('<div class="alert alert-danger">' + data.message
                            + '</div>');
                    }
                }
            });
        }
    };

    Search.init();
});

